//
//  JWShopAdvisoryVC.m
//  jinwang
//
//  Created by jzb on 2018/3/23.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWShopAdvisoryVC.h"
#import "JWShopAdvisoryCell1.h"
#import "JWShopAdvisoryCell2.h"
#import "JWShopAdvisoryCell3.h"
#import "JWShopAdvisoryModel.h"
#import "JWShopArticleInfoVC.h"

@interface JWShopAdvisoryVC ()

@property (nonatomic,strong)JWShopAdvisoryCell1 *cell1;
@property (nonatomic,strong)JWShopAdvisoryCell2 *cell2;
@property (nonatomic,strong)JWShopAdvisoryCell3 *cell3;

@property (nonatomic,strong) NSMutableArray *ListArr;

@property (nonatomic,assign) int page;

@end

@implementation JWShopAdvisoryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    [self setUpTableView];
    [self makeUrl_mall_articleList];
    
}
//配置tableView
-(void)setUpTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //注册cell
    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
    [self registerCellWithCellClassArray:@[NSStringFromClass([JWShopAdvisoryCell1 class])]];
    [self registerCellWithCellClassArray:@[NSStringFromClass([JWShopAdvisoryCell2 class])]];
    [self registerCellWithCellClassArray:@[NSStringFromClass([JWShopAdvisoryCell3 class])]];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       [self makeUrl_mall_articleList];
    }];
    self.tableView.mj_footer  = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        _page++;
        [self makeUrl_mall_articleList];
    }];
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _ListArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JWShopAdvisoryModel *model = _ListArr[indexPath.row];
    if (model.imgs.count == 0) {
        self.cell1 = [tableView dequeueReusableCellWithIdentifier:[JWShopAdvisoryCell1 getCellReuseIdentifier]];
        self.cell1.contentView.backgroundColor = [UIColor clearColor];
        self.cell1.model = model;
        return self.cell1;
    }else if (model.imgs.count > 0 &&model.imgs.count < 3){
        self.cell2 = [tableView dequeueReusableCellWithIdentifier:[JWShopAdvisoryCell2 getCellReuseIdentifier]];
        self.cell2.contentView.backgroundColor = [UIColor clearColor];
        self.cell2.model = model;
        return self.cell2;
    }else{
        self.cell3 = [tableView dequeueReusableCellWithIdentifier:[JWShopAdvisoryCell3 getCellReuseIdentifier]];
        self.cell3.contentView.backgroundColor = [UIColor clearColor];
        self.cell3.model = model;
        return self.cell3;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JWShopAdvisoryModel *model = _ListArr[indexPath.row];

    CGSize size = CGSizeMake(kScreenWidth - 20, MAXFLOAT);//设置高度宽度的最大限度
    CGRect article_titleRect = [model.article_title boundingRectWithSize:size options:NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16]} context:nil];
    if (model.imgs.count == 0) {
        return 70+article_titleRect.size.height;
    }else if (model.imgs.count > 0 &&model.imgs.count < 3){
       
        return 115;
    }else{
        
        return 145+article_titleRect.size.height;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JWShopArticleInfoVC *vc = [JWShopArticleInfoVC new];
    JWShopAdvisoryModel *model = _ListArr[indexPath.row];
    vc.article_id = [NSString stringWithFormat:@"%@",model.article_id];
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)makeUrl_mall_articleList{
    [[DDNetworkManagerDate makeUrl_mall_articleListWithtoken:k_user_token page:[NSString stringWithFormat:@"%d",_page]] post_RequestFinshSuccess:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if ([self requestFinshCode:responseObject isShowMessage:NO] == 1) {
            
            NSLog(@"====>>%@",responseObject);
//            _ListArr = [NSMutableArray new];
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            NSMutableArray *arr = [NSMutableArray array];
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                JWShopAdvisoryModel *model = [JWShopAdvisoryModel yy_modelWithDictionary:obj];
                [arr addObject:model];
            }];
//            _ListArr = arr.mutableCopy;
            if (_page == 1) {
                _ListArr = arr.mutableCopy;
            }else{
                [_ListArr addObjectsFromArray:arr];
            }
        }
        //刷新
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self showBottomMessag:@"网络请求失败"];
    }];
}

-(NSMutableArray *)ListArr{
    if (!_ListArr) {
        _ListArr = [NSMutableArray array];
    }
    return _ListArr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
